package org.jeecg.modules.wlhy.hywarningmessage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wlhy.hywarningmessage.entity.HyWarningMessage;
import org.jeecg.modules.wlhy.hywarningmessage.mapper.HyWarningMessageMapper;
import org.jeecg.modules.wlhy.hywarningmessage.service.IHyWarningMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 告警消息
 * @Author: duanlikao
 * @Date:   2020-12-22
 * @Version: V1.0
 */
@Service
public class HyWarningMessageServiceImpl extends ServiceImpl<HyWarningMessageMapper, HyWarningMessage>
        implements IHyWarningMessageService {

    @Resource
    private HyWarningMessageMapper hyWarningMessageMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(HyWarningMessageServiceImpl.class);

    @Override
    public HyWarningMessage getMessageByObjectIdAndMessageType(String objectId, Integer messageType) {
        return hyWarningMessageMapper.getMessageByObjectIdAndMessageType(objectId, messageType);
    }

    @Override
    public Result <?> getMessageByUserId(String userId) {
        Result<JSONObject> result = new Result<JSONObject>();
        List<HyWarningMessage> hyWarningMessages = hyWarningMessageMapper.getMessageByUserId(userId);
        return Result.ok(hyWarningMessages);
    }

    /**
     * 保存告警信息
     * @param hyWarningMessage
     * @return
     */
    @Override
    public boolean saveWarningMessage(HyWarningMessage hyWarningMessage){
        if(hyWarningMessage == null){
            return false;
        }
//        hyWarningMessage.setCreateBy("systemUser");
//        hyWarningMessage.setCreateTime(new Date());

        HyWarningMessage hyWarningMessageDb = getMessageByObjectIdAndMessageType(hyWarningMessage.getObjectId(),hyWarningMessage.getMessageType());
        boolean  falg;
        if(hyWarningMessageDb == null){
            falg =  save(hyWarningMessage);
        }else {
            hyWarningMessage.setId(hyWarningMessageDb.getId());
            falg = updateById(hyWarningMessage);
        }
        return falg;
    }

}
